'use strict';

const Controller = require('../lib/controller');

module.exports = class FeedController extends Controller {
  async index() {
    let strSQL;
    let sqlWHERE = '';

    // Generate a list of hidden categories
    for (let i = 0; i < this.ctx.app.theCache.categories.length; i++) {
      if (this.ctx.app.theCache.categories[i].hidden) {
        sqlWHERE += ' AND tLog.log_catID<>' + this.ctx.app.theCache.categories[i].id;
      }
    }

    if (this.ctx.request.query.q === 'comment') {

      // Check article id
      let articleId = this.ctx.request.query.id;
      if (articleId) {
        articleId = this.ctx.helper.checkInt(articleId);
        if (articleId > 0) {
          sqlWHERE += ' AND tLog.log_id=' + articleId;
        }
      }

      // Load Comments from DB when needed
      let comments;
      if (this.ctx.request.query.id > 0) {
        strSQL = 'SELECT tComm.comm_id, tComm.log_id, tComm.comm_author, tComm.comm_content, tComm.comm_postTime, tComm.comm_ubbFlags, tLog.log_title FROM blog_article tLog, blog_comment tComm WHERE tLog.log_id=tComm.log_id AND tLog.log_mode=1 AND tLog.log_id=' + articleId + ' AND tComm.comm_hidden=false' + sqlWHERE + ' ORDER BY tComm.comm_postTime DESC';
        comments = this.ctx.app.connBlog.query(strSQL, 10, 1);
        if (!comments) comments = [];
        for (let i = 0; i < comments.length; i++) {
          comments[i] = {
            id: comments[i].comm_id,
            log_id: comments[i].log_id,
            author: comments[i].comm_author,
            content: comments[i].comm_content,
            postTime: new Date(comments[i].comm_posttime),
            log_title: comments[i].log_title,
            ubbFlags: comments[i].comm_ubbflags,
          };
        }

      } else {
        comments = this.ctx.app.theCache.recentComments;
      }

      // Output RSS 2 Comments
      this.ctx.set('Content-Type', 'text/xml');
      return await this.ctx.render('feed/commentRss2.ejs', { comments });
    }

    // Check category id
    let categoryId = this.ctx.request.query.cat;
    if (categoryId) {
      categoryId = this.ctx.helper.checkInt(categoryId);
      if (categoryId > 0) {
        sqlWHERE += ' AND tLog.log_catID=' + categoryId;
      }
    }

    // Check if output selected content only
    if (this.ctx.request.query.selected === 'true') {
      sqlWHERE += ' AND tLog.log_selected=true';
    }

    // Load articles from DB when needed
    let articles;
    if (categoryId > 0 || this.ctx.request.query.selected === 'true') {
      strSQL = 'SELECT tLog.log_id, tLog.log_catid, tLog.log_title, tLog.log_authorid, tLog.log_author, tLog.log_mode, tLog.log_ubbFlags, tLog.log_postTime, tLog.log_content0 FROM blog_article tLog WHERE tLog.log_mode=1' +
        sqlWHERE + ' ORDER BY tLog.log_postTime DESC';
      articles = await this.ctx.app.connBlog.query(strSQL, 10, 1);
      if (!articles) articles = [];
    } else {
      articles = this.ctx.app.theCache.recentArticles;
    }

    let view = 'rss2';
    let contentType = 'xml';
    if (this.ctx.request.query.type === 'js') {
      view = 'feed';
      contentType = 'javascript';
    }

    this.ctx.set('Content-Type', `text/${contentType}`);
    return await this.ctx.render(`feed/${view}.ejs`, { arrArticle: articles });
  }
};
